import json

from flask_sqlalchemy.pagination import QueryPagination

from model import StuInfo


class MyJsonEncode(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj,QueryPagination):
            result = {
                'items':obj.items,
                'page':obj.page,
                'pages':obj.pages,
                'per_page':obj.per_page,
                'total':obj.total,
                'first':obj.first,
                'has_next':obj.has_next,
                'has_prev':obj.has_prev,
                'next_num':obj.next_num,
                'prev_num':obj.prev_num,
                'last':obj.last
            }
            return result
        elif isinstance(obj,StuInfo):
            result = {
                'sid':obj.sid,
                'name': obj.name,
                'sex': obj.sex,
                'class_name': obj.class_name,
                'college_name': obj.college_name,
                'email': obj.email,
                'grade': obj.grade,
                'major_name': obj.major_name,
                'phone': obj.phone,
                'password': obj.password
            }
            return result
        else:
            return super(MyJsonEncode,self).default(obj)
